<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\Product;

class Products extends Controller
{
    public function __construct(){

    }
    public function index()
    {
        $user_id = 1;

        $prod = new Product();

        $prods = $prod->getAllProducts();

        return view('index',['data' => $prods,'user_id'=>$user_id]);
    }

    public function add_product(){

        return view('add_product');
    }

    public function do_add_product(){
        $prod = new Product();

        $prod->name = input('post.name');
        $prod->user_id = 1;

        try {
            $res = $prod->save();
        }catch (\Exception $e) {
            $this->error($e->getMessage());
            exit;
        }

        if($res){
            $this->redirect('index');
        }else{
            $this->error('添加产品出错');
        }
    }
}